package com.webbit.swing;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

/**
 *
 * @author webbyit
 */
public class MainFrame extends javax.swing.JFrame {

    /** Creates new form MainFrame */
    public MainFrame() {
        setNimbusLookAndFeel();
        initComponents();
    }

    /**
     * Attempt to set the system state to Nimbus, otherwise choose the default
     * platform L&F
     */
    private void setNimbusLookAndFeel() {

        // Use longer but future proof method for setting up l&f
        UIManager.LookAndFeelInfo lafinfo[] = UIManager.getInstalledLookAndFeels();
        boolean nimbusfound = false;
        int nimbusindex = 0;

        for (int i = 0; i < lafinfo.length; i++) {
            if (lafinfo[i].getClassName().toLowerCase().contains("nimbus")) {
                nimbusfound = true;
                nimbusindex = i;
            }
        }


        String lf;
        if (nimbusfound) {
            lf = lafinfo[nimbusindex].getClassName();
        } else {
            lf = UIManager.getCrossPlatformLookAndFeelClassName();
        }

        try {
            // Exceptions here highlight a platform problem and hence are reported to the server (via 'error' level)
            UIManager.setLookAndFeel(lf);
        } catch (ClassNotFoundException ex) {
            System.err.println(lf + " look and feel not found");
        } catch (InstantiationException ex) {
            System.err.println(lf + " look and feel instantiation failure");
        } catch (IllegalAccessException ex) {
            System.err.println(lf + " look and feel instantiation illegal access");
        } catch (UnsupportedLookAndFeelException ex) {
            System.err.println(lf + " look and feel not supported");
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar = new JMenuBar();
        fileMenu = new JMenu();
        exitMI = new JMenuItem();
        helpMenu = new JMenu();
        aboutMI = new JMenuItem();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(400, 300));

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setMnemonic('F');
        fileMenu.setText("File");
        fileMenu.setName("fileMenu"); // NOI18N

        exitMI.setMnemonic('E');
        exitMI.setText("Exit");
        exitMI.setName("exitMI"); // NOI18N
        exitMI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exitMIActionPerformed(evt);
            }
        });
        fileMenu.add(exitMI);

        menuBar.add(fileMenu);

        helpMenu.setMnemonic('H');
        helpMenu.setText("Help");
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMI.setMnemonic('A');
        aboutMI.setText("About");
        aboutMI.setName("aboutMI"); // NOI18N
        aboutMI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                aboutMIActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMI);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMIActionPerformed(ActionEvent evt) {//GEN-FIRST:event_exitMIActionPerformed
        dispose();
    }//GEN-LAST:event_exitMIActionPerformed

    private void aboutMIActionPerformed(ActionEvent evt) {//GEN-FIRST:event_aboutMIActionPerformed
        AboutDialog ad = new AboutDialog(this, true, "1.0", "14:03:01 15th March 2011",
                "© 2011 Webby IT Ltd");
        ad.setVisible(true);
    }//GEN-LAST:event_aboutMIActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected JMenuItem aboutMI;
    protected JMenuItem exitMI;
    protected JMenu fileMenu;
    protected JMenu helpMenu;
    protected JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables
}
